package com.sma.controller;


import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.sma.pojo.Role;
import com.sma.service.ExcelExportService;
import com.sma.service.RoleService;
import com.sma.view.ExcelView;
import com.sma.vo.PageParams;
import com.sma.vo.RoleParams;

@Controller
@RequestMapping("/role")
// 设置Session属性
@SessionAttributes(names = "role", types = Role.class)
public class RoleController {

    // 角色服务
    @Autowired
    private RoleService roleService = null;

    /**
     * 测试ModelMap
     * @param id 角色编号
     * @param modelMap ModelMap类型
     * @return ModelAndView
     */
    @RequestMapping(value = "/modelmap/{id}", method = RequestMethod.GET)
    public ModelAndView getRoleByModelMap(
            @PathVariable("id") Long id, ModelMap modelMap) {
        Role role = roleService.getRole(id);
        ModelAndView mv = new ModelAndView();
        modelMap.addAttribute("role", role);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    /**
     * 测试Model
     * @param id 角色编号
     * @param model Model类型
     * @return ModelAndView
     */
    @RequestMapping(value = "/model/{id}", method = RequestMethod.GET)
    public ModelAndView getRoleByModel(
            @PathVariable("id") Long id, Model model) {
        Role role = roleService.getRole(id);
        ModelAndView mv = new ModelAndView();
        model.addAttribute("role", role);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping(
            value = "/mv/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView getRoleByMv(
            @PathVariable("id") Long id, ModelAndView mv) {
        Role role = roleService.getRole(id);
        mv.addObject("role", role);
        mv.addObject("id", id);
        // 跳转到具体的页面（/WEB-INF/jsp/session_show.jsp）
        mv.setViewName("session_show");
        return mv;
    }

    @RequestMapping(value = "/excel/list", method = RequestMethod.GET)
    public ModelAndView export() {
        //模型和视图
        ModelAndView mv = new ModelAndView();
        //Excel视图，并设置自定义导出接口
        ExcelView ev = new ExcelView("role-list", exportService(), "所有角色.xlsx");
        //设置SQL后台参数
        RoleParams roleParams = new RoleParams();
        //限制1万条
        PageParams page = new PageParams();
        page.setStart(0);
        page.setLimit(10000);
        roleParams.setPageParams(page);
        //查询
        List<Role>roleList = roleService.findRoles(roleParams);
        //加入数据模型
        mv.addObject("roleList", roleList);
        mv.setView(ev);
        return mv;
    }

    @SuppressWarnings({ "unchecked"})
    private ExcelExportService exportService() {
        //使用Lambda表达式自定义导出excel规则
        return (Map<String, Object> model, Workbook workbook) -> {
            //获取用户列表
            List<Role>roleList = (List<Role>) model.get("roleList");
            //生成Sheet
            Sheet sheet= workbook.createSheet("所有角色");
            //加载标题
            Row title = sheet.createRow(0);
            title.createCell(0).setCellValue("编号");
            title.createCell(1).setCellValue("名称");
            title.createCell(2).setCellValue("备注");
            //便利角色列表，生成一行行的数据
            for (int i=0; i<roleList.size(); i++) {
                Role role = roleList.get(i);
                int rowIdx = i + 1;
                Row row = sheet.createRow(rowIdx);
                row.createCell(0).setCellValue(role.getId());
                row.createCell(1).setCellValue(role.getRoleName());
                row.createCell(2).setCellValue(role.getNote());
            }
        };
    }
}
